﻿using WoodAdmin.Models;
using WoodAdmin.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;

namespace WoodAdmin.Services
{
	public class UserInfoService : BaseService<UserInfo>
	{
        /// <summary>
        ///拼接用户层级结构
        /// </summary>
        /// <param name="ib">推广代理号</param>
        /// <param name="userid">新用户ID</param>
        /// <returns></returns>
        public string GetPath(int? ib,int userid)
		{
			if (ib == null)
			{
				return "/"+userid+"/";
			}
			UserInfo ibUserInfo = FindEntity(ib);
			return ibUserInfo.Dep + userid + "/";
		}

		/// <summary>
		/// 获取用户全部下级
		/// </summary>
		/// <param name="userid">要获取下级用户列表的用户ID</param>
		/// <returns></returns>
		public List<UserInfo> GetAllSubUserInfo(string userid)
		{
			var dbUserinfo = FindEntity(userid);
			return Queryable(a => SqlFunc.Contains(a.Dep, dbUserinfo.Dep) && !SqlFunc.Equals(a.UserID,userid));
		}


        public void DemoTest()
        {
            List<SysGroup> rs = Db.Queryable<SysGroup, SysNode>((a, b) => new JoinQueryInfos(JoinType.Left, a.ID == b.GroupID))
                                  .Select<SysGroup>()
                                  .ToList();

            var rs2 = Db.Queryable<SysGroup, SysNode>((a, b) => new JoinQueryInfos(JoinType.Left, a.ID == b.GroupID))
                        .Select((a, b) => new { a.Name, b.NodeLevel })
                        .ToList();

            using var db = DbContext.GetIntance();

            var tranRs = db.Ado.UseTran(() =>
            {
                //事务内容
            });
            if (tranRs.IsSuccess)
            {
                //事务返回值
                var rsData = tranRs.Data;
            }
        }
    }
}